﻿<UserControl x:Class="MiniUML.View.Views.DocumentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:aero="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
             xmlns:local="clr-namespace:MiniUML.View.Views"
             xmlns:utilities="clr-namespace:MiniUML.View.Utilities"
             MinHeight="100" MinWidth="100">
    
    <UserControl.Resources>

        <OuterGlowBitmapEffect x:Key="SubtleShadow" GlowColor="Black" GlowSize="2" Opacity="0.15"/>
        
        <!-- ScrollViewerTemplate -->
        <ControlTemplate x:Key="ScrollViewerTemplate" TargetType="{x:Type ScrollViewer}">

            <ControlTemplate.Resources>

                <LinearGradientBrush x:Key="RightShadowBrush" StartPoint="1,0" EndPoint="0,0">
                    <GradientStop Offset="0.00" Color="#30000000"/>
                    <GradientStop Offset="1.00" Color="#00000000"/>
                </LinearGradientBrush>

                <LinearGradientBrush x:Key="BottomShadowBrush" StartPoint="0,1" EndPoint="0,0">
                    <GradientStop Offset="0.00" Color="#30000000"/>
                    <GradientStop Offset="1.00" Color="#00000000"/>
                </LinearGradientBrush>

                <LinearGradientBrush x:Key="LeftShadowBrush" StartPoint="0,0" EndPoint="1,0">
                    <GradientStop Offset="0.00" Color="#30000000"/>
                    <GradientStop Offset="1.00" Color="#00000000"/>
                </LinearGradientBrush>

                <LinearGradientBrush x:Key="TopShadowBrush" StartPoint="0,0" EndPoint="0,1">
                    <GradientStop Offset="0.00" Color="#30000000"/>
                    <GradientStop Offset="1.00" Color="#00000000"/>
                </LinearGradientBrush>

            </ControlTemplate.Resources>

            <Grid Background="{DynamicResource Style.Brushes.DocumentView.Background}">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <Border Grid.Row="0" Grid.Column="0" ClipToBounds="True" BorderBrush="Black">
                    <Grid>

                        <!-- ScrollContentPresenter -->
                        <ScrollContentPresenter x:Name="_scrollContentPresenter"/>

                        <!-- Busy overlay -->
                        <Grid x:Name="_busyOverlay" Visibility="Hidden">
                            <Rectangle Fill="White" Opacity="0.8"/>

                            <Border Padding="20"
                                    MaxWidth="300"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    BorderThickness="2"
                                    CornerRadius="3"
                                    BorderBrush="#F0F5F5F5"
                                    Background="#F0FAFAFA"
                                    BitmapEffect="{StaticResource SubtleShadow}">

                                <StackPanel>

                                    <TextBlock x:Name="_busyOverlayStatusText"
                                               Foreground="Gray"
                                               BitmapEffect="{StaticResource SubtleShadow}"
                                               FontSize="30"
                                               TextWrapping="Wrap"/>

                                    <TextBlock x:Name="_busyOverlayHelpText"
                                               Foreground="Gray"
                                               BitmapEffect="{StaticResource SubtleShadow}"
                                               FontSize="15"
                                               TextWrapping="Wrap"
                                               Text="Please wait..."/>
                                </StackPanel>
                                
                            </Border>
                            
                        </Grid>

                    </Grid>
                </Border>

                <!-- Approximate an inner glow effect using gradient brushes. -->
                <Rectangle Grid.Row="0" Grid.Column="0" Width="10" HorizontalAlignment="Right" Fill="{StaticResource RightShadowBrush}"/>
                <Rectangle Grid.Row="0" Grid.Column="0" Height="10" VerticalAlignment="Bottom" Fill="{StaticResource BottomShadowBrush}"/>
                <Rectangle Grid.Row="0" Grid.Column="0" Width="10" HorizontalAlignment="Left" Fill="{StaticResource LeftShadowBrush}"/>
                <Rectangle Grid.Row="0" Grid.Column="0" Height="10" VerticalAlignment="Top" Fill="{StaticResource TopShadowBrush}"/>

                <ScrollBar Name="PART_VerticalScrollBar"
                           Grid.Row="0" Grid.Column="1"
                           Margin="2" 
                           Opacity="0.6"
                           Background="#20FFFFFF"
                           Value="{TemplateBinding VerticalOffset}"
                           Maximum="{TemplateBinding ScrollableHeight}"
                           ViewportSize="{TemplateBinding ViewportHeight}"
                           Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>

                <ScrollBar Name="PART_HorizontalScrollBar"
                           Grid.Row="1" Grid.Column="0"
                           Margin="2"
                           Orientation="Horizontal"
                           Opacity="0.6"
                           Background="#20FFFFFF"
                           Value="{TemplateBinding HorizontalOffset}"
                           Maximum="{TemplateBinding ScrollableWidth}"
                           ViewportSize="{TemplateBinding ViewportWidth}"
                           Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
            </Grid>

            <!-- Enable overlay when the DocumentDataModel is not ready. -->
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding Path='dm_DocumentDataModel.State'}" Value="Loading">
                    <Setter TargetName="_scrollContentPresenter" Property="IsHitTestVisible" Value="False"/>
                    <Setter TargetName="_busyOverlay" Property="Visibility" Value="Visible"/>
                    <Setter TargetName="_busyOverlayStatusText" Property="Text" Value="Loading document"/>
                    <Setter TargetName="_busyOverlayHelpText" Property="Text" Value="Please wait..."/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path='dm_DocumentDataModel.State'}" Value="Saving">
                    <Setter TargetName="_scrollContentPresenter" Property="IsHitTestVisible" Value="False"/>
                    <Setter TargetName="_busyOverlay" Property="Visibility" Value="Visible"/>
                    <Setter TargetName="_busyOverlayStatusText" Property="Text" Value="Saving document"/>
                    <Setter TargetName="_busyOverlayHelpText" Property="Text" Value="Please wait..."/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path='dm_DocumentDataModel.State'}" Value="Invalid">
                    <Setter TargetName="_scrollContentPresenter" Property="IsHitTestVisible" Value="False"/>
                    <Setter TargetName="_busyOverlay" Property="Visibility" Value="Visible"/>
                    <Setter TargetName="_busyOverlayStatusText" Property="Text"  Value="Error in document"/>
                    <Setter TargetName="_busyOverlayStatusText" Property="Foreground" Value="#FFFF8080"/>
                    <Setter TargetName="_busyOverlayHelpText" Property="Text" Value="The document is in an invalid state."/>
                </DataTrigger>
            </ControlTemplate.Triggers>

        </ControlTemplate>
        
    </UserControl.Resources>

    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- ScollViewer -->
        <ScrollViewer x:Name="_scrollViewer"
                      Grid.Row="0" Grid.Column="0"
                      HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Auto"
                      Template="{StaticResource ScrollViewerTemplate}">

            <!-- ViewBox -->
            <Viewbox Margin="15" VerticalAlignment="Center" HorizontalAlignment="Center">

                <Viewbox.LayoutTransform>
                    <ScaleTransform ScaleX="{Binding ElementName=_zoomSlider, Path=Value}"
                                    ScaleY="{Binding ElementName=_zoomSlider, Path=Value}"/>
                </Viewbox.LayoutTransform>

                <Grid>

                    <!-- Page border and shadow -->
                    <aero:SystemDropShadowChrome Opacity="0.5"/>
                    <Border Width="{Binding Path=dm_DocumentDataModel.DocumentRoot.Attribute[PageWidth].Value}"
                            Height="{Binding Path=dm_DocumentDataModel.DocumentRoot.Attribute[PageHeight].Value}"
                            SnapsToDevicePixels="True"
                            BorderThickness="4"
                            CornerRadius="4"
                            BorderBrush="#50000000"
                            Background="#FFF5F5F5">

                        <Grid>
                            
                            <!-- CanvasView border and background -->
                            <Border x:Name="_marginBorder"
                                    Margin="{Binding Path=dm_DocumentDataModel.DocumentRoot.Attribute[PageMargins].Value}"
                                    SnapsToDevicePixels="True"
                                    CornerRadius="3"
                                    BorderThickness="1"                                    BorderBrush="#FFF0F0F0"
                                    Background="White"/>

                            <!-- CanvasView -->
                            <local:CanvasView x:Name="_documentVisual"
                                              Padding="{Binding Path=_DocumentViewModel.dm_DocumentDataModel.DocumentRoot.Attribute[PageMargins].Value}"
                                              Background="Transparent"
                                              DataContext="{Binding Path=vm_CanvasViewModel}"/>
                        </Grid>
                    
                    </Border>

                </Grid>

            </Viewbox>
            
        </ScrollViewer>

        <!-- Zoom Slider -->
        <Border Margin="25,25,0,0"
                Padding="5,2,5,2"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                SnapsToDevicePixels="True"
                BorderThickness="1"
                CornerRadius="2"
                BorderBrush="#80707070"
                Background="{DynamicResource Style.Brushes.DocumentView.ZoomSliderBackground}">

            <Border.BitmapEffect>
                <DropShadowBitmapEffect Color="Black" Opacity="0.5" ShadowDepth="2"/>
            </Border.BitmapEffect>

            <StackPanel>
                
                <TextBlock x:Name="_zoomTextBlock" HorizontalAlignment="Center" Foreground="White" FontSize="9" Text="100%"/>
                
                <Slider x:Name="_zoomSlider" Height="120" Orientation="Vertical" Opacity="0.7"
                        Minimum="0.25" Maximum="4.0" Value="1" TickFrequency="0.25" TickPlacement="BottomRight"
                        ValueChanged="_zoomSlider_ValueChanged"/>
                
            </StackPanel>
            
        </Border>

        <!-- XML View -->
        <local:XmlView x:Name="_xmlView" Grid.Row="1" Grid.Column="0"
                       DataContext="{Binding Path=vm_XmlViewModel}"/>
    </Grid>

</UserControl>